//
//  MBBaseServer.m
//  MBObjectServer
//
//  Created by admin on 3/3/14.
//  Copyright (c) 2014 MBpayer. All rights reserved.
//

#import "MBBaseServer.h"
#import "EPOSBaseResult.h"
#import "EPOSBasePost.h"

//操作日志消息名
#define CONTROLMSG_WEAK_NOTIFICATION @"ControlMsg_weak"
@implementation MBBaseServer
@dynamic baseclient;
@synthesize delegate;
- (id)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}


- (void)setRequestStyle:(NSString*)style andSetPathString:(NSString*)path andSetHeadString:(NSString*)head andActionName:(NSString*)action andSetMainDictionary:(NSMutableDictionary*)dictionary
{
    paths=path;
    if ([action isEqualToString:@"PayRequest"])
    {
         baseclient = [MBBaseHttpClient sharedPayHttpClient];
        if (!self.isStartLoad) {
             [SVProgressHUD show];
        }
        baseclient.delegate = self;
        EPOSBasePost *postData = [[EPOSBasePost alloc]init];
        postData.ActionName = action;
        postData.Pars = dictionary;
        NSMutableDictionary *postPrepare = [[NSMutableDictionary alloc]init];
        postPrepare = [EPOSBasePost getDictionaryFromEPOSBasePost:postData];
        NSString *postString = [[NSString alloc] initWithData:[self toJSONData:postPrepare] encoding:NSUTF8StringEncoding];
        NSMutableDictionary *postDictionary = [[NSMutableDictionary alloc]init];
        if (head==nil )
        {
            [postDictionary setObject:postString forKey:@"inData"];
        }
        else
        {
            [postDictionary setObject:postString forKey:head];
        }
        if ([style isEqualToString:@"POST"]||style==nil)
        {
            [baseclient postDataWithPath:path withAction:action  WithParameters:postDictionary];
        }
    }
    else
    {
         baseclient = [MBBaseHttpClient sharedMBBaseHttpClient];
        if (!self.isStartLoad) {
            [SVProgressHUD show];
        }
        baseclient.delegate = self;
        EPOSBasePost *postData = [[EPOSBasePost alloc]init];
        postData.ActionName = action;
        postData.Pars = dictionary;
        NSMutableDictionary *postPrepare = [[NSMutableDictionary alloc]init];
        postPrepare = [EPOSBasePost getDictionaryFromEPOSBasePost:postData];
        NSString *postString = [[NSString alloc] initWithData:[self toJSONData:postPrepare] encoding:NSUTF8StringEncoding];
        NSMutableDictionary *postDictionary = [[NSMutableDictionary alloc]init];
//        if (head==nil )
//        {
            [postDictionary setObject:postString forKey:@"inData"];
//        }
//        else
//        {
//            [postDictionary setObject:postString forKey:head];
//        }
        if ([style isEqualToString:@"POST"]||style==nil)
        {
            NSLog(@"%@",postString);
            [baseclient postDataWithPath:path withAction:head  WithParameters:postDictionary];
            
            
        }
        else if ([head isEqualToString:@"GET"])
        {
            [baseclient updateDataWithPath:path withAction:action WithParameters:postDictionary];
        }
    }
    
}

-(void)setBaseclient:(MBBaseHttpClient *)newBaseclient{
    self.isDismiss=YES;
    [baseclient cancelAllHTTPOperationsWithMethod:@"POST" path:paths];
}

#pragma mark //将字典或者数组转化为JSON串
// 将字典或者数组转化为JSON串
- (NSData *)toJSONData:(id)theData{
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        return jsonData;
    }else{
        return nil;
    }
}

#pragma mark delegate;
-(void)MBBaseHttpClient:(MBBaseHttpClient *)client WithPath:(NSString *)path didUpdateWithData:(id)operationdata{
    if (!self.isStartLoad) {
         [SVProgressHUD dismiss];
    }
    NSDictionary * dic = (NSDictionary *)operationdata;
    NSLog(@"%@",dic);
    
    NSString * stringmsg = [dic objectForKey:@"Message"];
    //    发送消息用来更新操作日志
    [[NSNotificationCenter defaultCenter]
     postNotificationName:CONTROLMSG_WEAK_NOTIFICATION
     object:stringmsg];
    [self.delegate receiveData:dic  WithPath:path];

}
//-(void)CanclResult{
//    [baseclient cancelAllHTTPOperationsWithMethod:Nil path:Nil];
//}
-(void)MBBaseHttpClient:(MBBaseHttpClient *)client WithPath:(NSString *)path didFailWithError:(NSError *)error{
    NSLog(@"%@",error);
    if (!self.isDismiss) {
         [SVProgressHUD showErrorWithStatus:@"网络请求错误"];
    }
    if ([self.delegate respondsToSelector:@selector(FailureData: WithError:)]) {
        [self.delegate FailureData:path WithError:error];
    }
}
- (void)setRequestMBStyle:(NSString*)style andSetPathString:(NSString*)path andSetHeadString:(NSString*)head andActionName:(NSString*)action andSetMainDictionary:(NSMutableDictionary*)dictionary  block:(void(^)(NSDictionary * responseData))processResult failblock:(void(^)(NSError *error))failresult
{
//    baseclient = [MBBaseHttpClient sharedPayHttpClient];
    paths=path;
    if ([action isEqualToString:@"PayRequest"])
    {
        baseclient = [MBBaseHttpClient sharedPayHttpClient];
        
    }
    else
    {
        baseclient = [MBBaseHttpClient sharedMBBaseHttpClient];
        
    }
    if (!self.isStartLoad) {
        [SVProgressHUD show];
    }
//    [baseclient postDataWithPath:path withAction:action  WithParameters:dictionary];
//     [baseclient updateDataWithPath:path withAction:action WithParameters:dictionary];
    if ([style isEqualToString:@"POST"]||style==nil)
    {
        [baseclient postDataWithPath:path
                          withAction:action
                      WithParameters:dictionary
                               block:^(id JSON){
                                   if (!self.isStartLoad) {
                                       [SVProgressHUD dismiss];
                                   }
                                   NSDictionary *dic=(NSDictionary *)JSON;
                                   processResult(dic);
                                   NSLog(@"dic = %@",dic);
                               }failblock:^(NSError *error)
         {
             failresult(error);
         }];
    }
    else{
        
    }

    
}

- (void)setRequestStyle:(NSString*)style andSetPathString:(NSString*)path andSetHeadString:(NSString*)head andActionName:(NSString*)action andSetMainDictionary:(NSMutableDictionary*)dictionary block:(void(^)(NSDictionary * responseData))processResult failblock:(void(^)(NSError *error))failresult
    {
        paths=path;
            baseclient = [MBBaseHttpClient sharedPayHttpClient];
            
        if (!self.isStartLoad) {
            [SVProgressHUD show];
        }
        
        EPOSBasePost *postData = [[EPOSBasePost alloc]init];
        postData.ActionName = action;
        postData.Pars = dictionary;
        NSMutableDictionary *postPrepare = [[NSMutableDictionary alloc]init];
        postPrepare = [EPOSBasePost getDictionaryFromEPOSBasePost:postData];
        NSString *postString = [[NSString alloc] initWithData:[self toJSONData:postPrepare] encoding:NSUTF8StringEncoding];
        NSMutableDictionary *postDictionary = [[NSMutableDictionary alloc]init];
        if (head==nil )
        {
            [postDictionary setObject:postString forKey:@"inData"];
        }
        else
        {
            [postDictionary setObject:postString forKey:head];
        }
        MBLog(@"%@?inData=%@",path,postString);
        if ([style isEqualToString:@"POST"]||style==nil)
        {
            [baseclient postDataWithPath:path
                              withAction:action
                          WithParameters:postDictionary
                                   block:^(id JSON){
                                       
                                       if (!self.isStartLoad) {
                                           [SVProgressHUD dismiss];
                                       }
                                       NSDictionary *dic=(NSDictionary *)JSON;
                                       processResult(dic);
                                       NSLog(@"dic = %@",dic);
            }failblock:^(NSError *error)
            {
                failresult(error);
            }];
        }

    }

@end
